package cn.edu.sjtu.stap.cfg;

import cn.edu.sjtu.stap.graph.*;

public class CfgFlow extends GraphEdge {
    private static final long serialVersionUID = 1L;

    private static long edgeCount = 0;

    private CfgFlowKind kind;

    protected CfgFlow(CfgFlowKind kind) {
        super(++edgeCount);
        this.kind = kind;
    }

    public CfgFlow(CfgFlowKind kind, CfgNode from, CfgNode to) {
        super(++edgeCount, from, to);
        setKind(kind);
    }
    
    public void setKind(CfgFlowKind kind) {
        if (null == kind) {
            throw new NullPointerException("Flow kind should not be null!");
        }
        this.kind = kind;
    }

    public CfgFlowKind getKind() {
        return kind;
    }
    
    public String getName() {
        return kind.getName();
    }

    public void setSourceNode(GraphNode node) {
        if (!(node instanceof CfgNode)) {
            throw new IllegalArgumentException();
        }
        super.setSourceNode(node);
    }

    public void setDestinationNode(GraphNode node) {
        if (!(node instanceof CfgNode)) {
            throw new IllegalArgumentException();
        }
        super.setDestinationNode(node);
    }

    public CfgNode getSourceNode() {
        return (CfgNode) super.getSourceNode();
    }

    public CfgNode getDestinationNode() {
        return (CfgNode) super.getDestinationNode();
    }

    public String toString() {
        StringBuffer s = new StringBuffer();
        s.append("(Flow Id: ");
        s.append(this.getId());
        s.append(", Name: ");
        s.append(getName());
        s.append(", From: (Node Id: ");
        s.append(this.getSourceNode().getId());
        s.append("), To: (Node Id: ");
        s.append(this.getDestinationNode().getId());
        s.append("))");

        return s.toString();
    }
}
